home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / emulator / bsvc-1.000 / bsvc-1 / bsvc-1.0.4 / src / Assemblers / hecasm / asm1.c < prev    next >
C/C++ Source or Header  |  1995-07-26  |  5KB  |  239 lines

  1. /* asm1.c main ____________ assembler */
  2.  
  3. #include "asm.h"
  4.  
  5. /*
  6.  * This is main.
  7.  * It reads the command line (eg. $ asm -ln infile),
  8.  * collects up a source file name (eg. infile),
  9.  * sets option flags (-l means list file AND load file
  10.  * enabled; -ln means list but no load file), and calls
  11.  * the assembler proper.
  12.  */
  13. main(argc, argv)
  14. char *argv[];
  15. {
  16. register int i, c;
  17. register char *p;
  18. char *file;
  19.  
  20. file = NULL;
  21.  
  22. /* process all of the command line arguments */
  23. for(i=1; i<argc; ++i) 
  24.     {
  25.     p = argv[i];
  26.  
  27.     /* if the first character is a dash this must be an option */
  28.     if(*p++ == '-') 
  29.         {
  30.         while(c = *p++)
  31.         switch(c) 
  32.             {
  33.         /* option to turn on listing */
  34.             case 'l':
  35.             case 'L':
  36.                 ++lflag;
  37.                 break;
  38.  
  39.         /* option to turn off object file generation */
  40.             case 'n':
  41.             case 'N':
  42.                 ++nflag;
  43.                 break;
  44.  
  45.         /* unrecognized option send an error message and leave */
  46.             default:
  47.                 usage();
  48.             }
  49.         } 
  50.     else 
  51.         if(file != NULL)
  52.             /* No file name found on command line */
  53.             usage();
  54.         else
  55.         /* pointer to name of file */
  56.             file = argv[i];
  57.     }
  58.  
  59. /* check if a flag was used more than once - this is picky */
  60. if ((lflag > 1) || (nflag > 1))
  61.     usage();
  62.  
  63. /* no file name found */
  64. if(file == NULL)
  65.     usage();
  66.  
  67. /* Call file assembler */
  68. assemble(file);       
  69. }
  70.  
  71.  
  72. /*
  73.  * Assemble a file. 
  74.  */
  75.  
  76.  
  77. assemble(file)
  78. char *file;
  79. {
  80. char fn[40];
  81. char sn[40];
  82. int enablst;  
  83.  
  84. /* If enablst=1 listing is enabled, this flag works with .list directive */
  85. enablst = lflag; 
  86.  
  87. /* make file name - if no extension exists add the default (.asm), otherwise 
  88. * leave it alone.  ifp becomes the pointer to the source file input buffer.  
  89. The file is opened for reading. */
  90. name(sn, file, "hec", 0);
  91. if((ifp = fopen(sn, "r")) == NULL) 
  92.     {
  93.     fprintf(stderr, "%s: cannot open\n", sn);
  94.     exit(1);
  95.     }
  96.  
  97. /* if a listing file is desired make the file name and open the file for 
  98. writing.  lfp becomes the pointer to the list file output buffer. */
  99. if(lflag) 
  100.     {
  101.     /* If -l flag is set, create an output file for the listing */
  102.     /* replace current extension with .lis */
  103.     name(fn, file, "lis", 1);
  104.     if((lfp = fopen(fn, "w")) == NULL) 
  105.         {
  106.         fprintf(stderr, "%s: cannot create\n", fn);
  107.         exit(1);
  108.     }
  109.     }
  110.  
  111. /* if code is to be generated make the file name and open the file for 
  112. writing.  ofp becomes the pointer to the object-code file output buffer. */
  113. if(!nflag) 
  114.     {
  115.     /* If -n flag is NOT set, open an output file for code */
  116.     /* replace current extension with .obj */
  117.     name(fn, file, "lod", 1);
  118.     if((ofp = fopen(fn, "w")) == NULL) 
  119.         {
  120.         fprintf(stderr, "%s: cannot create\n", fn);
  121.         exit(1);
  122.         }
  123.     }
  124.  
  125. /* initialize the user symbol table.  It is a hash table so all valid */
  126. /* flags must be marked false and the location counter must be entered */
  127. /* as a symbol */
  128. initust();
  129.  
  130. /* this is a two pass assembler */
  131. for(pass=0; pass<2; ++pass) 
  132.     {
  133.     lineno = 0;
  134.  
  135.     /* dot is a pointer to the location counter symbol */
  136.     /* this initializes its value before each pass */
  137.     dot->s_type = S_DEF | S_VALID;
  138.     dot->s_value = 0;
  139.     dot->s_flag = SF_ASG;
  140.  
  141.     /* read in source file one line at a time */
  142.     while(fgets(sbuf, SRCMAX, ifp) != NULL) 
  143.         {
  144.         ++lineno;
  145.  
  146.     /* reset pointers to source code buffer, output code buffer, and */
  147.     /* the error buffer */
  148.         sptr = sbuf;
  149.         cptr = cbuf;
  150.         eptr = ebuf;
  151.  
  152.     /* assemble the line in the source buffer and put the code in the */
  153.     /* code buffer.  Any errors are put in the error buffer */
  154.         asmline();
  155.  
  156.     /* output the listing file line by line on the second pass */
  157.         if(pass) 
  158.             if (lflag)
  159.                 outlisting();
  160.         }
  161.  
  162.     /* After completing the first pass, close the source file and then
  163.     * reopen it */
  164.     if(!pass) 
  165.         {
  166.         fclose(ifp);
  167.         ifp = fopen(sn,"r");
  168.         }
  169.     }
  170.  
  171. /* output the symbol table if listing is enabled at this point in the file */
  172. /* enablst reflects the state of the .list directive */
  173. if(enablst)
  174.     symprt();
  175.  
  176. /* if object code is to be output write out the last line of code */
  177. if(!nflag)
  178.         {
  179.     cflush();    /* Flush the code buffer if -l flag set   */
  180.         }
  181. }
  182.  
  183.  
  184. /*
  185.  * If the user screws up, put out
  186.  * a usage message.
  187.  * Then quit.
  188.  * Not much sense staying around.
  189.  */
  190. usage()
  191. {
  192. fprintf(stderr, "Usage:  asm [-ln] file\n");
  193. exit(1);
  194. }
  195.  
  196.  
  197. /*
  198.  * Build file names.
  199.  * The mode argument is either 0  which means default, or 1 which
  200.  * means replace with the file extension passed in the argument list.
  201.  * In the default case, the extension added to the file will be the 
  202.  * extension provided by the "file" name or, if it has none, the extension
  203.  * in the "type" string.
  204.  */
  205. name(fn, file, type, mode)
  206. char *fn, *file, *type;
  207. int mode;
  208. {
  209. register char *p1, *p2;
  210.  
  211. p1 = fn;
  212. p2 = file;
  213. while((*p1 = *p2++) && (*p1 != '.'))
  214.     p1++;
  215.  
  216. if(mode == 0) 
  217.     {
  218.     if(*p1 == '.') 
  219.         {
  220.         while(*++p1 = *p2++);
  221.         p1++;
  222.         }
  223.     else 
  224.         {
  225.         *p1++ = '.';
  226.         p2 = type;
  227.         while(*p1++ = *p2++);
  228.         }
  229.     } 
  230. else 
  231.     {
  232.     *p1++ = '.';
  233.     p2 = type;
  234.     while(*p1++ = *p2++);
  235.     }
  236. *p1 = '\0';
  237. }
  238.  
  239.